import logging
import os
import sys
from datetime import datetime

class AppLogger:
    def __init__(self, log_file="remote_controller.log"):
        self.log_file = log_file
        self.setup_logger()
    
    def setup_logger(self):
        """设置日志系统"""
        # 创建日志目录
        log_dir = os.path.dirname(self.log_file)
        if log_dir and not os.path.exists(log_dir):
            os.makedirs(log_dir)
        
        # 配置日志
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[
                logging.FileHandler(self.log_file, encoding='utf-8'),
                logging.StreamHandler(sys.stdout)  # 同时输出到控制台
            ]
        )
        
        self.logger = logging.getLogger("RemoteController")
    
    def info(self, message):
        import threading
        threading.Thread(target=self.logger.info, args=(message,)).start()
    
    def error(self, message):
        import threading
        threading.Thread(target=self.logger.error, args=(message,)).start()
    
    def warning(self, message):
        import threading
        threading.Thread(target=self.logger.warning, args=(message,)).start()
    
    def exception(self, message):
        import threading
        threading.Thread(target=self.logger.exception, args=(message,)).start()

# 全局日志实例
logger = AppLogger()